Passed
Push — next ( b0778e...143128 )
by Roy
01:26
created

OSPaths.ts ➔ joinPathToBase   B

Complexity

Conditions 7

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 2
rs 8
c 0
b 0
f 0
cc 7
1
// # spell-checker:ignore AllUsersProfile HomeDrive HomePath LocalAppData UserProfile WinDir falsey
2
3
import { Platform } from '../platform-adapters/_base';
4
5
export type OSPaths = {
6
	new (): OSPaths;
7
	(): OSPaths;
8
	readonly home: () => string | undefined;
9
	readonly temp: () => string;
10
};
11
12
function isEmpty(s: string | null | undefined): boolean {
13
	return !s; // reminder: JS "falsey" == [undefined, null, NaN, 0, '', false]
14
}
15
16
const AdaptingIsWinOS = (adapter_: Platform.Adapter) => /^win/i.test(adapter_.process.platform);
17
18
const AdaptingNormalizePath = (adapter_: Platform.Adapter) => {
19
	return (path_: string | undefined): string | undefined => {
20
		return path_ ? adapter_.path.normalize(adapter_.path.join(path_, '.')) : void 0;
21
	};
22
};
23
24
const AdaptingHome = (adapter_: Platform.Adapter) => {
25
	const { env, os, path } = adapter_;
26
	const isWinOS = AdaptingIsWinOS(adapter_);
27
	const normalizePath = AdaptingNormalizePath(adapter_);
28
29
	const posix = () =>
30
		normalizePath((typeof os.homedir === 'function' ? os.homedir() : void 0) || env.get('HOME'));
31
32
	const windows = () => {
33
		const priorityList = [
34
			typeof os.homedir === 'function' ? os.homedir() : void 0,
35
			env.get('USERPROFILE'),
36
			env.get('HOME'),
37
			env.get('HOMEDRIVE') || env.get('HOMEPATH')
38
				? path.join(env.get('HOMEDRIVE') || '', env.get('HOMEPATH') || '')
39
				: void 0,
40
		];
41
		return normalizePath(priorityList.find((v) => !isEmpty(v)));
42
	};
43
44
	return isWinOS ? windows : posix;
45
};
46
47
const AdaptingTemp = (adapter_: Platform.Adapter) => {
48
	const { env, os, path } = adapter_;
49
	const isWinOS = AdaptingIsWinOS(adapter_);
50
	const normalizePath = AdaptingNormalizePath(adapter_);
51
	function joinPathToBase(base: string | undefined, segments: ReadonlyArray<string>) {
52
		return base ? path.join(base, ...segments) : void 0;
53
	}
54
55
	const posix = () => {
56
		const fallback = '/tmp';
57
		const priorityList = [
58
			typeof os.tmpdir === 'function' ? os.tmpdir() : void 0,
59
			env.get('TMPDIR'),
60
			env.get('TEMP'),
61
			env.get('TMP'),
62
		];
63
		return normalizePath(priorityList.find((v) => !isEmpty(v))) || fallback;
64
	};
65
66
	const windows = () => {
67
		const fallback = 'C:\\Temp';
68
		const priorityListLazy = [
69
			os.tmpdir,
70
			() => env.get('TEMP'),
71
			() => env.get('TMP'),
72
			() => joinPathToBase(env.get('LOCALAPPDATA'), ['Temp']),
73
			() => joinPathToBase(AdaptingHome(adapter_)(), ['AppData', 'Local', 'Temp']),
74
			() => joinPathToBase(env.get('ALLUSERSPROFILE'), ['Temp']),
75
			() => joinPathToBase(env.get('SystemRoot'), ['Temp']),
76
			() => joinPathToBase(env.get('windir'), ['Temp']),
77
			() => joinPathToBase(env.get('SystemDrive'), ['\\', 'Temp']),
78
		];
79
		const v = priorityListLazy.find((v) => v && !isEmpty(v()));
80
		return (v && normalizePath(v())) || fallback;
81
	};
82
83
	return isWinOS ? windows : posix;
84
};
85
86
export function OSPathsAdaptionBuilder_(adapter_: Platform.Adapter): OSPaths {
87
	// eslint-disable-next-line functional/no-class
88
	class OSPaths_ {
89
		constructor() {
90
			const OSPaths = function () {
91
				return new OSPaths_() as OSPaths;
92
			};
93
94
			OSPaths.home = AdaptingHome(adapter_);
95
			OSPaths.temp = AdaptingTemp(adapter_);
96
97
			return OSPaths;
98
		}
99
	}
100
101
	return new OSPaths_() as OSPaths;
102
}
103